#ifndef MODBUSTRANSFER_P_H
#define MODBUSTRANSFER_P_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief modbus规约封装类
*/
#include "phylink.h"
#include "sys_inc.h"
#include "modbustransfer.h"

class ModbusTransferPrivate {
    friend class ModbusTransfer;
public:
    ModbusTransferPrivate(IPhyLink *dev, int mode=ModbusTransfer::RTU);
    ~ModbusTransferPrivate();
    int sendCmdAndRecv(const ModbusCmdInfo &cmd, uint8_t *recv_buf);
    int setYKCmd(const ModbusYKCmdInfo &cmd);
    uint8_t encode(const ModbusCmdInfo &cmd, uint8_t *buf, uint8_t buf_size);
    uint8_t encode(const ModbusYKCmdInfo &cmd, uint8_t *buf, uint8_t buf_size);
    int parseRTU(const uint8_t *rx_data, uint16_t rx_len, uint8_t slave_id, uint8_t func);
    bool verifyCRC(const uint8_t *rx_data, uint16_t rx_len);
    int recvRTU(uint8_t slave_id, uint8_t func, uint8_t *rd, uint16_t rl, uint16_t &pos);
    int recvTCP(const uint8_t *td, uint16_t tl, uint8_t *rd, uint16_t rl, uint16_t &pos);
    int readAndWrite(const uint8_t *td, uint16_t tl, uint8_t *rd, uint16_t rl);
    int continueRecvData(uint8_t *rd, uint16_t rl, uint16_t &pos);

    IPhyLink *dev_;
    int mode_;
    const ComSettings *const com_settings_;
};


#endif
